package com.lekang.le.service.impl;

import com.lekang.ZcqEntity.*;
import com.lekang.le.service.wxBillZcqService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.le.mapper.wxBillZcqMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class wxBillZcqServiceImpl implements wxBillZcqService {
    @Resource
    private wxBillZcqMapper wxBillZcqMapper;
    @Override
    public List<elderlySysZcq> getElderlyList(Long wxUserId) {
        List<elderlySysZcq> elderlyList = wxBillZcqMapper.getElderlyList(wxUserId);
        return elderlyList;
    }







    @Override
    public List<BillVOZcq> getBillListZcq(Long wxUserId, Long elderlyId, String transactionStatus) {
        // 1. 根据微信用户ID查询绑定的所有老人ID（过滤掉未绑定老人的记录）
        List<Long> boundElderlyIds = wxBillZcqMapper.selectElderlyIdsByWxUserIdZcq(wxUserId);
        if (CollectionUtils.isEmpty(boundElderlyIds)) {
            return List.of(); // 无绑定老人，返回空列表
        }
        // 2. 构建最终查询的老人ID范围（优先使用前端传递的老人ID，无则用绑定的所有老人ID）
        Long targetElderlyId = elderlyId;
        List<Long> queryElderlyIds = boundElderlyIds;
        if (targetElderlyId != null) {
            // 验证传递的老人ID是否属于当前微信用户绑定的老人（防止越权）
            if (boundElderlyIds.contains(targetElderlyId)) {
                queryElderlyIds = List.of(targetElderlyId);
            } else {
                return List.of(); // 传递的老人ID不属于该用户，返回空列表
            }
        }

        // 3. 调用Mapper查询账单列表
        return wxBillZcqMapper.selectBillListByConditionZcq(
                queryElderlyIds,
                transactionStatus
        );
    }

    @Override
    public int payBill(Long billId) {
      int i=  wxBillZcqMapper.payBill(billId);
        return i;
    }


}
